/*
 * Does the logging stuf
 * Licensed by the Bunie Developmentgroup
 */
package infoserver;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * Creates a logfile and puts information in it. @sinces v0.9
 *
 * @version v0.9
 * @author Patrick Wobben
 */

public class Log {

    static private FileHandler fileTxt;
    static private SimpleFormatter formatterTxt;
    static private Logger log = Logger.getLogger("");
    private final static Logger LOGGER = Logger.getLogger(Log.class.getName());
    
    public final static int INFO = 1;
    public final static int WARNING = 2;
    public final static int SEVERE = 3;
    public final static int CONFIG = 4;
    
    /**
    * Setups the file, sets limits on size and number of files.
    * @author Patrick Wobben
    * @since v0.9
    * @version: v0.95
    */
    public static void setup() {
        try {
            log.setLevel(Level.INFO);
            fileTxt = new FileHandler("infoserver-log.txt", 1000000, 1, true);

            formatterTxt = new SimpleFormatter();
            fileTxt.setFormatter(formatterTxt);
            log.addHandler(fileTxt);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /**
     * Method to print messages in the System.out and in the file.
     * @author Patrick Wobben
     * @param message Message to be printed.
     * @param level Level of the logmessage.
     *
     */
    
    public static void print(String message, int level) {
        System.out.println("Server: " + message);
        switch (level){
            case (Log.INFO): LOGGER.info(message);
                break;
            case (Log.WARNING): LOGGER.warning(message);
                break;
            case (Log.SEVERE): LOGGER.severe(message);
                break;
            case (Log.CONFIG): LOGGER.config(message);
                break;
            }
    }
}